package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.lebu.controller.req.AddPersonMotionReq;
import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.controller.req.UpdatePersonMotionReq;
import cn.jcby.app.lebu.controller.res.PersonMotionStatRes;
import cn.jcby.app.lebu.dao.UserDao;
import cn.jcby.app.lebu.dao.UserPersonMotionDao;
import cn.jcby.app.lebu.entity.UserEntity;
import cn.jcby.app.lebu.entity.UserPersonMotionEntity;
import cn.jcby.app.lebu.service.UserPersonMotionService;
import cn.jcby.app.lebu.utils.Constants;
import cn.jcby.app.lebu.utils.LocalDateUtils;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.UserInfoUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service("userPersonMotionService")
@Slf4j
public class UserPersonMotionServiceImpl extends ServiceImpl<UserPersonMotionDao, UserPersonMotionEntity> implements UserPersonMotionService {

    @Autowired
    private UserDao userDao;
    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<UserPersonMotionEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<UserPersonMotionEntity>lambdaQuery()
        );

        return new PageUtils(page);
    }
    @Override
    public void add(AddPersonMotionReq req){
        //判断是否有未完成的记录
        UserPersonMotionEntity otherMotion = this.getOne(Wrappers.<UserPersonMotionEntity>lambdaQuery()
                .eq(UserPersonMotionEntity::getUserId,UserInfoUtil.getUserId())
                .eq(UserPersonMotionEntity::getStatus,0)) ;
        Assert.isNull(otherMotion,"请先完成未完成的记录"); ;

        UserPersonMotionEntity userPersonMotion = new UserPersonMotionEntity() ;
        userPersonMotion.setUserId(UserInfoUtil.getUserId()) ;
        userPersonMotion.setMotionType(req.getMotionType());
        userPersonMotion.setBeginLocation(req.getBeginLocation());
        userPersonMotion.setEndLocation(req.getEndLocation());
        userPersonMotion.setPlanWholeCourse(req.getPlanWholeCourse());
        userPersonMotion.setStatus(0);
        userPersonMotion.setCreateTime(new Date());
        userPersonMotion.setUpdateTime(new Date());
        userPersonMotion.setBeginTime(new Date());
        userPersonMotion.setDel(0);
        //添加坐标数据
        if(req.getBeginLocationPoint()!= null && req.getEndLocationPoint()!= null){
            userPersonMotion.setBeginLocationPoint(req.getBeginLocationPoint());
            userPersonMotion.setEndLocationPoint(req.getEndLocationPoint());
        }
        if (req.getRoute()!= null){
            userPersonMotion.setRoute(req.getRoute());
        }
        log.info("添加运动记录参数：{}", JSONObject.toJSONString(userPersonMotion)) ;
        this.baseMapper.saveRecord(userPersonMotion) ;
    }

    @Override
    public void updateBy(UpdatePersonMotionReq req){
        req.setUserId(UserInfoUtil.getUserId());
        this.baseMapper.updateRecord(req);
        //更新总里程
        UserEntity user= new UserEntity();
        user.setId(UserInfoUtil.getUserId());
        user.setTotalMileage(req.getMileage());
        userDao.updateMileage(user);
    }

    @Override
    public PersonMotionStatRes stat(String motionType) {
        //统计总里程
        PersonMotionStatRes res = new PersonMotionStatRes();
        res.setThisMonthMileage("0米");
        res.setTotalMileage("0米");
        Map<String, Object> map = getMap(new QueryWrapper<UserPersonMotionEntity>().select("sum(mileage) as mileage")
                .eq("motion_type", motionType)
                .gt("mileage", 0)
                .eq("user_id", UserInfoUtil.getUserId()));
        if (map != null && map.get("mileage") != null){
            res.setTotalMileage(Constants.getMileageStr((BigDecimal)map.get("mileage")));

            //统计本月里程
            //本月开始时间
            Date beginTime = LocalDateUtils.thisMonthDay() ;
            Map<String, Object> thisMonthMap = getMap(new QueryWrapper<UserPersonMotionEntity>().select("sum(mileage) as mileage")
                    .eq("motion_type", motionType)
                    .gt("mileage", 0)
                    .eq("user_id", UserInfoUtil.getUserId())
                    .ge("begin_time",beginTime)
            );
            if (thisMonthMap != null && thisMonthMap.get("mileage") != null){
                res.setThisMonthMileage(Constants.getMileageStr((BigDecimal) thisMonthMap.get("mileage")));
            }
        }

        //统计本月里程
        return res;
    }
}